USE Lib
GO

---------------------------------------------------------------------------
-- BEGIN MAINTENANCE WRAPPER
----------------------------
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'FN' AND name = 'fn_quarter_month') BEGIN
	DROP  FUNCTION  tsql.fn_quarter_month
	PRINT '<<< DROP FUNCTION tsql.fn_quarter_month - Completed with SUCCESS >>>'
END
GO


/*================================================================================================
Name:			tsql.fn_month_last_day
Type:			User defined function 
Owner:					
Description:	Returns the month of quarter
Parameters:		@date = date to get day for
Dependens on:  	tsql.fn_quarter_frst_day
Usage:			SELECT [tsql].[fn_quarter_month] (GETDATE())
--------------------------------------------------------------------------------------------------
Project:		http://sqllib.codeplex.com/
Help:			http://sqllib.codeplex.com/wikipage?title=fn_month_last_day&referringTitle=Documentation		
Version:		20121018
SrcCtrlApp:		SVN
SrcCtrlPath:	http://sqllib.codeplex.com/SourceControl/list/changesets
--------------------------------------------------------------------------------------------------
History:		10/18/2012 - Adrian E Dudau - Function created
================================================================================================*/


CREATE FUNCTION tsql.fn_quarter_month (@date DATETIME)
RETURNS INT
AS

BEGIN
	RETURN DATEDIFF(MONTH,tsql.fn_quarter_first_day(@date),@date)
END

GO

---------------------------------------------------------------------------
-- BEGIN MAINTENANCE WRAPPER
-- Verify completion
----------------------------
IF EXISTS(SELECT * FROM sysobjects WHERE type = 'FN' AND name = 'fn_quarter_month') BEGIN 
	PRINT '<<< CREATE FUNCTION tsql.fn_quarter_month - Completed with SUCCESS >>>'
END

ELSE BEGIN 
	PRINT '<<< CREATE FUNCTION tsql.fn_quarter_month - Completed with ERROR >>>'
END


GO
----------------------------
-- END MAINTENANCE WRAPPER
---------------------------------------------------------------------------

-- GRANT SELECT TO PUBLIC will be used at Db level



